/**
 * @author LKQ
 * @date 2022/3/1 13:52
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.sortString("leetcode"));
    }
    public String sortString(String s) {
        int[] cnt = new int[26];
        for(char c: s.toCharArray()) {
            cnt[c-'a']++;
        }
        StringBuilder ans = new StringBuilder();
        while (ans.length() < s.length()) {
            for(int i = 0 ; i < 26; i++) {
                if (cnt[i] > 0) {
                    ans.append((char) (i + 'a'));
                    cnt[i]--;
                }
            }
            for (int i = 25; i >= 0; i--) {
                if (cnt[i] > 0) {
                    ans.append((char) (i + 'a'));
                    cnt[i]--;
                }
            }
        }
        return ans.toString();
    }
}
